package com.example.kaogong.sys.controller;

import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.QiniuUtil;
import com.example.kaogong.common.utils.RespBean;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 七牛云 前端控制器
 * @author: 徐文升
 * @create: 2022-04-04 20:54
 **/

@RestController
@RequestMapping(value = "/qiniu")
@Api(tags = "七牛云接口")
@Slf4j
public class QiniuController {

    @Autowired
    private QiniuUtil qiniuUtil;

    @ApiOperation(value = "获取上传凭证token", notes = "同名文件覆盖上传")
    @GetMapping("/token/{fileName}")
    public RespBean getUploadToken(@PathVariable String fileName) {
        Assert.notBlank(fileName, new ParamsErrorException("文件名不能为空"));
        String token = qiniuUtil.generateUploadToken(fileName);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        RespBean respBean = RespBean.success(tokenMap);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
